package com.ymatou.userservice.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.userservice.parameter.AliUserRequestDtoBean;
import com.ymatou.userservice.parameter.BindAliUserRequestDtoBean;
import com.ymatou.userservice.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.userservice.service.BindAliUserRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.sqlwapper.YmtUsersWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.AppTypeChannel;
import com.ymttest.utils.constant.ECode;
import com.ymttest.utils.constant.ThirdPartyType;
/**
*绑定支付宝账号接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_BindAliUserRequestDto{
	private static BindAliUserRequestDtoBean bindaliuserrequestdtoBean;
	private static BindAliUserRequestDtoCall bindaliuserrequestdtoCall;
	private static YmtUsersWapper ymtUser;
	
	@BeforeClass
	public static void setUp() throws Exception {
		Logger.createResultFile("绑定支付宝账号接口");
		ymtUser = new YmtUsersWapper();
	}

	@Before
	public void caseUp() {
		bindaliuserrequestdtoBean=new BindAliUserRequestDtoBean();
		bindaliuserrequestdtoCall=new BindAliUserRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(bindaliuserrequestdtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindAliUserRequestDto_001() {
		Logger.start(true,"支付宝账号未绑定过，绑定成功");
		try {
			String ali = "NewBindAli_"+TestDataManager.getRandomNum(10);
			Logger.comment("验证该账号未绑定过");
			Map aliUser = ymtUser.getThirdPartyByThirdPartyId("Ymt_User_At_Alipay", "sUserId", ali);
			Logger.verifyIsNull(aliUser, "数据库中不存在该第三方账号");
			
			Logger.comment("前置准备：新创建一个用户");
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			
			Map result = bindAli(token, "mainsite", ali);
			Logger.verifyEquals(ECode.SUCCESS, ((Double)result.get("ResponseCode")).intValue()+"", "验证Code信息");
			Logger.verifyEquals(true, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("ok", result.get("Result"), "验证结果");
			
			Logger.comment("验证该账号不需要绑定手机");
			UserCallService.checkThirdPartyNeedBindMobile(ali, ThirdPartyType.ALI, ECode.USER_NO);
			Logger.comment("验证用该账号登陆成功");
			UserCallService.regByAli(ali, "",true, "登录成功");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindAliUserRequestDto_002() {
		Logger.start(true,"支付宝账号绑定过当前账号，返回已绑定过");
		try {
			String ali = "NewBindAli_"+TestDataManager.getRandomNum(10);
			Logger.comment("验证该账号未绑定过");
			Map aliUser = ymtUser.getThirdPartyByThirdPartyId("Ymt_User_At_Alipay", "sUserId", ali);
			Logger.verifyIsNull(aliUser, "数据库中不存在该第三方账号");
			
			Logger.comment("前置准备：新创建一个用户");
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			
			Map result = bindAli(token, "mainsite", ali);	
			Logger.verifyEquals(ECode.SUCCESS, String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			
			Logger.comment("再次绑定");
			result = bindAli(token, "mainsite", ali);
			Logger.verifyEquals("401", String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			Logger.verifyEquals(false, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("", result.get("Result"), "验证结果");
			Logger.verifyEquals("此支付宝账号已绑定", result.get("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindAliUserRequestDto_003() {
		Logger.start(true,"支付宝账号已绑定过其他账号，绑定失败");
		try {
			String ali = "NewBindAli_"+TestDataManager.getRandomNum(10);
			Logger.comment("验证该账号未绑定过");
			Map aliUser = ymtUser.getThirdPartyByThirdPartyId("Ymt_User_At_Alipay", "sUserId", ali);
			Logger.verifyIsNull(aliUser, "数据库中不存在该第三方账号");
			Logger.comment("用该账号注册一个新账号");
			UserCallService.regByAli(ali, TestDataManager.getPhoneNum(), true, "注册成功");
			
			Logger.comment("在另一个码头账号里绑定该支付宝");
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			
			Map result = bindAli(token, "mainsite", ali);
			Logger.verifyEquals("401",  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			Logger.verifyEquals(false, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("", result.get("Result"), "验证结果");
			Logger.verifyEquals("此支付宝账号已绑定", result.get("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindAliUserRequestDto_004() {
		Logger.start(true,"用户绑定了A支付宝账号->解绑A支付宝账号 ->用A登录失败 ->绑定A支付宝账号->绑定成功 ->用A登录成功");
		try {
			String ali = "NewBindAliA_"+TestDataManager.getRandomNum(10);
			Logger.comment("验证该账号未绑定过");
			Map aliUser = ymtUser.getThirdPartyByThirdPartyId("Ymt_User_At_Alipay", "sUserId", ali);
			Logger.verifyIsNull(aliUser, "数据库中不存在该第三方账号");
			
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			int userId = (Integer)user.get("userId");
			
			Logger.comment("在码头账号里绑定该支付宝");
			Map result = bindAli(token, "mainsite", ali);	
			Logger.verifyEquals(ECode.SUCCESS,  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			UserCallService.regByAli(ali, "",true, "登录成功");
			
			Logger.comment("解绑A支付宝");
			UserCallService.unbindThirdParty(userId, ali, ThirdPartyType.ALI);
			UserCallService.checkThirdPartyNeedBindMobile(ali, ThirdPartyType.ALI, ECode.USER_YES);;
			
			Logger.comment("绑定A支付宝");
			result = bindAli(token, "mainsite", ali);	
			Logger.verifyEquals(ECode.SUCCESS,  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			UserCallService.regByAli(ali, "",true, "登录成功");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindAliUserRequestDto_005() {
		Logger.start(true,"用户绑定了A支付宝账号->解绑A支付宝账号->绑定B支付宝账号->绑定成功->用A登录失败 ->用B登录成功");
		try {
			String ali = "NewBindAliA_"+TestDataManager.getRandomNum(10);
			Logger.comment("验证该账号未绑定过");
			Map aliUser = ymtUser.getThirdPartyByThirdPartyId("Ymt_User_At_Alipay", "sUserId", ali);
			Logger.verifyIsNull(aliUser, "数据库中不存在该第三方账号");
			
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			int userId = (Integer)user.get("userId");
			
			Logger.comment("在码头账号里绑定该支付宝A");
			Map result = bindAli(token, "mainsite", ali);
			Logger.verifyEquals(ECode.SUCCESS,  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			UserCallService.regByAli(ali, "",true, "登录成功");
			
			Logger.comment("解绑A支付宝");
			UserCallService.unbindThirdParty(userId, ali, ThirdPartyType.ALI);
			UserCallService.checkThirdPartyNeedBindMobile(ali, ThirdPartyType.ALI, ECode.USER_YES);;
			
			Logger.comment("绑定B支付宝");
			String aliB = "NewBindAliB_"+TestDataManager.getRandomNum(10);
			result = bindAli(token, "mainsite", aliB);	
			Logger.verifyEquals(ECode.SUCCESS,  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			UserCallService.regByAli(aliB, "",true, "登录成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindAliUserRequestDto_006() {
		Logger.start(false,"未输入token，失败");
		try {
			String ali = "NewBindAli_"+TestDataManager.getRandomNum(10);
			Map result = bindAli(null, "mainsite", ali);	
			Logger.verifyEquals(ECode.ERROR400, ((Double)result.get("ResponseCode")).intValue()+"", "验证Code信息");
			Logger.verifyEquals(false, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("", result.get("Result"), "验证结果");
			Logger.verifyEquals("错误的请求参数", result.get("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindAliUserRequestDto_007() {
		Logger.start(false,"未输入业务端类型，成功");
		try {
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			String ali = "NewBindAli_"+TestDataManager.getRandomNum(10);
			Map result = bindAli(token, "", ali);	
			Logger.verifyEquals(ECode.ERROR400, ((Double)result.get("ResponseCode")).intValue()+"", "验证Code信息");
			Logger.verifyEquals(false, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("", result.get("Result"), "验证结果");
			Logger.verifyEquals("错误的请求参数", result.get("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindAliUserRequestDto_008() {
		Logger.start(false,"未输入支付宝账号，失败");
		try {
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String token = user.get("token").toString();
			Map result = bindAli(token, "mainsite", null);	
			Logger.verifyEquals(ECode.ERROR400, ((Double)result.get("ResponseCode")).intValue()+"", "验证Code信息");
			Logger.verifyEquals(false, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("", result.get("Result"), "验证结果");
			Logger.verifyEquals("阿里Id不能为空", result.get("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindAliUserRequestDto_009() {
		Logger.start(true,"用户A绑定了A阿里账号->解绑A阿里账号->用户B绑定A阿里账号->用户B解绑A阿里账号");
		try {
			String phone = TestDataManager.getPhoneNum();
			Logger.comment("注册码头账号");
			Map userA = UserCallService.registerUser(null, phone,null);
			String tokenA = userA.get("token").toString();
			int userAId = (Integer)userA.get("userId");
			
			Logger.comment("绑定了A阿里账号");
			String aliA = "unbindali_"+TestDataManager.getRandomNum(10);
			Map result = bindAli(tokenA, "mainsite", aliA);
			Logger.verifyEquals(ECode.SUCCESS,  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			
			List<Map> thirdParty = new ArrayList<Map>();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.ALI, aliA, true);
			UserCallService.checkThirdPartyBindYmt(userAId, thirdParty);
			
			Logger.comment("解绑A阿里账号");
			result = UserCallService.unbindThirdParty(userAId, aliA, ThirdPartyType.ALI);
			Logger.verifyEquals(200d, result.get("ResponseCode"), "验证Code");
			Logger.verifyEquals(true, result.get("Success"), "验证是否成功");
			Logger.verifyEquals("ok", result.get("Result"), "验证Msg信息");
			thirdParty.clear();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.ALI, aliA, false);
			UserCallService.checkThirdPartyBindYmt(userAId, thirdParty);
			
			Logger.comment("用户B绑定A阿里账号");
			String phoneB = TestDataManager.getPhoneNum();
			Map userB = UserCallService.registerUser(null, phoneB,null);
			String tokenB = userB.get("token").toString();
			int userBId = (Integer)userB.get("userId");
			result = bindAli(tokenB, "mainsite", aliA);
			
			Logger.verifyEquals(ECode.SUCCESS,  String.valueOf(((Double)result.get("ResponseCode")).intValue()), "验证Code信息");
			thirdParty.clear();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.ALI, aliA, true);
			UserCallService.checkThirdPartyBindYmt(userBId, thirdParty);
			
			Logger.comment("解绑A阿里账号");
			result = UserCallService.unbindThirdParty(userBId, aliA, ThirdPartyType.ALI);
			Logger.verifyEquals(200d, result.get("ResponseCode"), "验证Code");
			Logger.verifyEquals(true, result.get("Success"), "验证是否成功");
			Logger.verifyEquals("ok", result.get("Result"), "验证Msg信息");
			thirdParty.clear();
			UserCallService.addThirdPartyAccount(thirdParty, ThirdPartyType.ALI, aliA, false);
			UserCallService.checkThirdPartyBindYmt(userBId, thirdParty);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindAliUserRequestDto_010() {
		Logger.start(true,"新Token格式支付宝账号未绑定过，绑定成功JIRACONN-2123");
		try {
			String ali = "NewBindAli_"+TestDataManager.getRandomNum(10);
			Logger.comment("验证该账号未绑定过");
			Map aliUser = ymtUser.getThirdPartyByThirdPartyId("Ymt_User_At_Alipay", "sUserId", ali);
			Logger.verifyIsNull(aliUser, "数据库中不存在该第三方账号");
			
			Logger.comment("前置准备：新创建一个用户");
			UserRegisterInfoRequestDtoBean rgbmbean = UserCallService.initUserRegInfo();
			rgbmbean.setTokenSource("app");
			Map user=UserCallService.registerByMobile(rgbmbean);
			String token = user.get("accesstoken").toString();
			
			bindaliuserrequestdtoBean=new BindAliUserRequestDtoBean();
			bindaliuserrequestdtoCall=new BindAliUserRequestDtoCall();
			bindaliuserrequestdtoBean.setToken(token);
			bindaliuserrequestdtoBean.setClientType("app");
			AliUserRequestDtoBean alib = createAliUserBean(ali);
			alib.setTokenSource("app");
			bindaliuserrequestdtoBean.setAliUser(alib);
			bindaliuserrequestdtoCall.setData(bindaliuserrequestdtoBean);
			bindaliuserrequestdtoCall.callService();
			Map result = bindaliuserrequestdtoCall.getMap();
			Logger.verifyEquals(ECode.SUCCESS, ((Double)result.get("ResponseCode")).intValue()+"", "验证Code信息");
			Logger.verifyEquals(true, result.get("Success"), "验证操作成功");
			Logger.verifyEquals("ok", result.get("Result"), "验证结果");
			
			Logger.comment("验证该账号不需要绑定手机");
			UserCallService.checkThirdPartyNeedBindMobile(ali, ThirdPartyType.ALI, ECode.USER_NO);
			Logger.comment("验证用该账号登陆成功");
			UserCallService.regByAli(ali, "",true, "登录成功");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private AliUserRequestDtoBean createAliUserBean(String aliUserId){
		AliUserRequestDtoBean aliuserregbean = new AliUserRequestDtoBean();
		aliuserregbean.setSAliUserId(aliUserId);
		aliuserregbean.setSRealName(aliUserId);
		aliuserregbean.setSEmail(TestDataManager.getEmail());
		aliuserregbean.setRegSource(1);
		aliuserregbean.setTerminalSource("ios");
		aliuserregbean.setAppTypeChanndel(AppTypeChannel.NEW_CHANNEL);
		aliuserregbean.setMobile("13339857810");
		aliuserregbean.setClientIp("120.1.1.1");
		aliuserregbean.setTokenTimeOut((new Date()).getTime());
		return aliuserregbean;
	}

	private Map bindAli(String token, String clientType, String ali) throws ClientProtocolException, IOException, InterruptedException{
		Thread.sleep(1000);
		bindaliuserrequestdtoBean=new BindAliUserRequestDtoBean();
		bindaliuserrequestdtoCall=new BindAliUserRequestDtoCall();
		bindaliuserrequestdtoBean.setToken(token);
		bindaliuserrequestdtoBean.setClientType(clientType);
		bindaliuserrequestdtoBean.setAliUser(createAliUserBean(ali));
		bindaliuserrequestdtoCall.setData(bindaliuserrequestdtoBean);
		bindaliuserrequestdtoCall.callService();
		return bindaliuserrequestdtoCall.getMap();
	}
	
}